package com.yami.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.dto.HotTopicDTO;
import com.yami.shop.bean.model.HotTopics;
import com.yami.shop.bean.model.TopicParticipation;
import com.yami.shop.bean.vo.TopicDetailVO;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.HotTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用于存储社区中的热门话题 前端控制器
 * </p>
 *
 * @author EXiaoKe
 * @since 2025-04-14
 */
@RestController
@RequestMapping("/social/topic")
public class HotTopicsController {

    @Autowired
    private HotTopicService hotTopicsService;

    /**
     * 分页查询话题
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('social:topic:page')")
    public ServerResponseEntity<IPage<HotTopicDTO>> page(HotTopics topic, PageParam<HotTopics> page) {
        return ServerResponseEntity.success(hotTopicsService.getTopicWithPosts(page, topic));
    }
    /**
     * 获取话题详情
     */

    @GetMapping("/detail/{topicId}")
   @PreAuthorize("@pms.hasPermission('social:topic:info')")
//    @PreAuthorize("true") // 放行所有请求

    public ServerResponseEntity<TopicDetailVO> detail(@PathVariable Long topicId) {
        return ServerResponseEntity.success(hotTopicsService.getTopicDetail(topicId));
    }

    /**
     * 新增话题
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('social:topic:create')")
//    @PreAuthorize("true") // 放行所有请求

    public ServerResponseEntity<Void> save(@RequestBody HotTopics topic) {
        hotTopicsService.save(topic);
        return ServerResponseEntity.success();
    }

    /**
     * 删除话题
     */
    @PreAuthorize("true") // 放行所有请求
    @DeleteMapping("/{topicId}")
//    @PreAuthorize("@pms.hasPermission('social:topic:delete')")
    public ServerResponseEntity<Void> delete(@PathVariable Long topicId) {
        hotTopicsService.removeById(topicId);
        return ServerResponseEntity.success();
    }


}

